<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
	$partyIdSelected              = isset($_REQUEST['partyId']) ? $_REQUEST['partyId'] : 0 ;
	$partyNameSelected            = "";
	$editArray['transactionId']   =0;
	$editArray['partyId']         = 0;
	$editArray['partyName']       = "";
	$editArray['jamaRupiya']      = "";
	$editArray['jamaVigat']       = "";
	$editArray['udhaarRupiya']    = "";
	$editArray['udhaarVigat']     = "";
	$editArray['udhaarVajan']     = "";
	$editArray['udhaarBhav']      = "";
  	
	if(isset($_POST['submitBtn']))
	{
	  $date             = $_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay'];
    $partyId          = (isset($_POST['partyId'])) && ($_POST['partyId'] != "") ? $_POST['partyId'] : 0 ;
    $jamaRupiya       = (isset($_POST['jamaRupiya'])) && ($_POST['jamaRupiya'] != "") ? $_POST['jamaRupiya'] : 0 ;
    $jamaVigat        = (isset($_POST['jamaVigat'])) && ($_POST['jamaVigat'] != "") ? $_POST['jamaVigat'] : 0 ;
    $udhaarRupiya     = (isset($_POST['udhaarRupiya'])) && ($_POST['udhaarRupiya'] != "") ? $_POST['udhaarRupiya'] : 0 ;
    $udhaarVigat      = (isset($_POST['udhaarVigat'])) && ($_POST['udhaarVigat'] != "") ? $_POST['udhaarVigat'] :"" ;
    $udhaarVajan      = (isset($_POST['udhaarVajan'])) && ($_POST['udhaarVajan'] != "") ? $_POST['udhaarVajan'] : 0 ;
    $udhaarBhav       = (isset($_POST['udhaarBhav'])) && ($_POST['udhaarBhav'] != "") ? $_POST['udhaarBhav'] : 0 ;
    $requireRemind    = (isset($_POST['status'])) && ($_POST['status'] != "") ? $_POST['status'] : "" ;
	  $remindDate       = $_POST['remindYear']."-".$_POST['remindMonth']."-".$_POST['remindDay'];
	  
	  /////////////
	  $updateRemindDate = "UPDATE party
	                         SET requireRemind = '".$requireRemind."',
	                             remindDate    = '".$remindDate."'
	                           WHERE partyId   = ".$partyId;
	  $updateRemindDateRes = mysql_query($updateRemindDate);
	  if(!$updateRemindDateRes)
	  {
	  	echo "UPDATE FAIL : ".$updateRemindDate;
	  }
	  /////////////
    
    if(isset($_POST['transactionId']) && $_POST['transactionId'] > 0)
	  {       
	    $udhaarVajan  = strlen($_POST['udhaarVajan']) > 0 ? $_POST['udhaarVajan'] : 0 ;
	    $udhaarBhav   = strlen($_POST['udhaarBhav']) > 0 ? $_POST['udhaarBhav'] : 0 ;
	    $udhaarRupiya = strlen($_POST['udhaarRupiya']) > 0 ? $_POST['udhaarRupiya'] : 0 ;
	    $jamaRupiya   = strlen($_POST['jamaRupiya']) > 0 ? $_POST['jamaRupiya'] : 0 ;
	    
	    $updateTrans = "UPDATE transaction
                              SET  partyId         = ".$_POST['partyId'].",
                                   jamaRupiya      = ".$jamaRupiya.",
  	                             	 udhaarRupiya  	 = ".$udhaarRupiya.",
   	                         	     udhaarVigat  	 = '".$_POST['udhaarVigat']."',
   	                         	     udhaarVajan  	 = ".$udhaarVajan.",
   	                         	     udhaarBhav  		 = ".$udhaarBhav."
   	                         WHERE transactionId   = ".$_REQUEST['transactionId'];
      $updateTransResult = mysql_query($updateTrans);
      if(!$updateTransResult)
      {
        die("Update Query Not Inserted : ".mysql_error());
      }
      else
      {
        header("Location:transaction.php");  
        exit();
      }
    }
    else
    {
      $insert = "INSERT INTO transaction(date,partyId,jamaRupiya,jamaVigat,udhaarRupiya,udhaarVigat,udhaarVajan,udhaarBhav)
                 VALUES('".$date."',".$partyId.",".$jamaRupiya.",'".$jamaVigat."',".$udhaarRupiya.",'".$udhaarVigat."',
                        ".$udhaarVajan.",".$udhaarBhav.")";
      $inResult = mysql_query($insert);
      if(!$inResult)
      {
        echo "error";
      }
    }
  }

  //edit Start
  if(isset($_REQUEST['transactionId']) && $_REQUEST['transactionId'] > 0)	
  {
    $editArray = array();
    $selectParty = "SELECT transactionId,date,transaction.partyId,jamaRupiya,jamaVigat,udhaarRupiya,udhaarVigat,udhaarVajan,udhaarBhav
                      FROM transaction
                     LEFT JOIN party ON party.partyId = transaction.partyId
                     WHERE transactionId = ".$_GET['transactionId'];
    $selectPartyRes = mysql_query($selectParty);
    while($editRow = mysql_fetch_array($selectPartyRes))
    {
    	$editArray['transactionId']   = $editRow['transactionId'];
    	$editArray['partyId']       = $editRow['partyId'];
    	$editArray['jamaRupiya']    = $editRow['jamaRupiya'];
    	$editArray['jamaVigat']     = $editRow['jamaVigat'];
    	$editArray['udhaarRupiya']  = $editRow['udhaarRupiya'];
    	$editArray['udhaarVigat']   = $editRow['udhaarVigat'];
    	$editArray['udhaarVajan']   = $editRow['udhaarVajan'];
    	$editArray['udhaarBhav']    = $editRow['udhaarBhav'];
    }
  }
  //edit end
    
  $transactionArray = array();
  $selectParty = "SELECT transactionId,transaction.partyId,date,partyName,jamaRupiya,jamaVigat,udhaarRupiya,udhaarVigat,udhaarVajan,udhaarBhav
                    FROM transaction
                    LEFT JOIN party ON party.partyId = transaction.partyId
                   ORDER BY transactionId DESC
                   LIMIT 5";
  $selectPartyRes = mysql_query($selectParty);
  $i=0;
  while($partyRow = mysql_fetch_array($selectPartyRes))
  {
  	$transactionArray[$i]['transactionId']    = $partyRow['transactionId'];
  	$transactionArray[$i]['partyId']          = $partyRow['partyId'];
  	$transactionArray[$i]['partyName']        = $partyRow['partyName'];
  	$transactionArray[$i]['jamaRupiya']       = $partyRow['jamaRupiya'];
  	$transactionArray[$i]['jamaVigat']        = $partyRow['jamaVigat'];
  	$transactionArray[$i]['udhaarRupiya']     = $partyRow['udhaarRupiya'];
  	$transactionArray[$i]['udhaarVigat']      = $partyRow['udhaarVigat'];
  	$transactionArray[$i]['udhaarVajan']      = $partyRow['udhaarVajan'];
  	$transactionArray[$i]['udhaarBhav']       = $partyRow['udhaarBhav'];
  	$i++;                                     
  }
  //List :End
  
  $partArray = array();
  $s = 0;
  $selectParty = "SELECT partyName
                    FROM party
                   WHERE partyId = ".$partyIdSelected;
  $selectPartyRes = mysql_query($selectParty);
  if($partyRow = mysql_fetch_array($selectPartyRes))
  {
  	$partyNameSelected = $partyRow['partyName'];
  }
  $statusArray[0]       = "N";
  $statusOutputArray[0] = "No";
  $statusArray[1]       = "Y";
  $statusOutputArray[1] = "Yes";
}
include("./bottom.php");
$smarty->assign("transactionArray",$transactionArray);
$smarty->assign("editArray",$editArray);
$smarty->assign("partyIdSelected",$partyIdSelected);
$smarty->assign("partyNameSelected",$partyNameSelected);
$smarty->assign("partArray",$partArray);
$smarty->assign("statusOutputArray",$statusOutputArray);
$smarty->assign("statusArray",$statusArray);
$smarty->display('transaction.tpl');
?>